package com.ruoyi.service;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.dto.ParkingSpaceDTO;
import com.ruoyi.entity.BizBusinessPktToCloud;
import com.ruoyi.entity.BizParkingSpace;
import com.ruoyi.entity.BizPriceParkingSpace;
import com.ruoyi.mapper.BizParkingSpaceMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.param.ParkingSpaceParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bean.BizParkingSpaceFrom;
import com.sxftech.resp.ResponseData;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 车位信息表 服务实现类
 * </p>
 *
 * @author liuxing
 * @since 2020-10-20
 */
@Service
public class BizParkingSpaceService extends ServiceImpl<BizParkingSpaceMapper, BizParkingSpace> {

	@Autowired
	private BizParkingSpaceMapper bizParkingSpaceMapper;
	@Autowired
	private BizPriceParkingSpaceService bizPriceParkingSpaceService;

	public ResponseData create(BizParkingSpaceFrom bizParkingSpace) {
		BizParkingSpace entity = transformationFrom(bizParkingSpace);
		this.save(entity);
		return ResponseData.success("新增成功");
	}
	
	public ResponseData modify(BizParkingSpaceFrom bizParkingSpace) {
		if(findById(bizParkingSpace.getId())== null) {
			return ResponseData.fail("没有找到对应记录");
		}
		BizParkingSpace entity = transformationFrom(bizParkingSpace);
		entity.setId(bizParkingSpace.getId());
		this.updateById(entity);
		return ResponseData.success("修改成功");
	}

	public BizParkingSpace transformationFrom(BizParkingSpaceFrom bizParkingSpace) {
		BizParkingSpace entity = new BizParkingSpace();
		entity.setPlCode(bizParkingSpace.getPlCode());
		entity.setSpaceCode(bizParkingSpace.getSpaceCode());
		entity.setZoneCode(bizParkingSpace.getZoneCode());
		entity.setPlZoneInZoneCode(bizParkingSpace.getPlZoneInZoneCode());
		entity.setPropertyPerson(bizParkingSpace.getPropertyPerson());
		entity.setUserPerson(bizParkingSpace.getUserPerson());
		entity.setUserPhone(bizParkingSpace.getUserPhone());
		entity.setNature(bizParkingSpace.getNature());
		entity.setIsShare(bizParkingSpace.getIsShare());
		entity.setSpaceType(bizParkingSpace.getSpaceType());
		entity.setSpaceState(bizParkingSpace.getSpaceState());
		entity.setRemark(bizParkingSpace.getRemark());
		entity.setTransverseAxis(bizParkingSpace.getTransverseAxis());
		entity.setPortraiAxis(bizParkingSpace.getPortraiAxis());
		entity.setIsOccupy(bizParkingSpace.getIsOccupy());
		entity.setSelfIsShare(bizParkingSpace.getSelfIsShare());
		entity.setSpacePosition(bizParkingSpace.getSpacePosition());
		entity.setDepartmentId(bizParkingSpace.getDepartmentId());
		entity.setIsDeleted(bizParkingSpace.getIsDeleted());
		entity.setPlatformIsShare(bizParkingSpace.getPlatformIsShare());
		entity.setShareChangeTime(bizParkingSpace.getShareChangeTime());
		entity.setReserveTempCount(bizParkingSpace.getReserveTempCount());
		entity.setReserveTempMaxCount(bizParkingSpace.getReserveTempMaxCount());
		entity.setRankDirection(bizParkingSpace.getRankDirection());
		entity.setRankOrder(bizParkingSpace.getRankOrder());
		entity.setCreator(bizParkingSpace.getCreator());
		entity.setCreateTime(bizParkingSpace.getCreateTime());
		entity.setModifier(bizParkingSpace.getModifier());
		entity.setModifyTime(bizParkingSpace.getModifyTime());
		return entity;
	}

	public BizParkingSpaceFrom transformationBean(BizParkingSpace bizParkingSpace) {
		BizParkingSpaceFrom from = new BizParkingSpaceFrom();
		from.setPlCode(bizParkingSpace.getPlCode());
		from.setSpaceCode(bizParkingSpace.getSpaceCode());
		from.setZoneCode(bizParkingSpace.getZoneCode());
		from.setPlZoneInZoneCode(bizParkingSpace.getPlZoneInZoneCode());
		from.setPropertyPerson(bizParkingSpace.getPropertyPerson());
		from.setUserPerson(bizParkingSpace.getUserPerson());
		from.setUserPhone(bizParkingSpace.getUserPhone());
		from.setNature(bizParkingSpace.getNature());
		from.setIsShare(bizParkingSpace.getIsShare());
		from.setSpaceType(bizParkingSpace.getSpaceType());
		from.setSpaceState(bizParkingSpace.getSpaceState());
		from.setRemark(bizParkingSpace.getRemark());
		from.setTransverseAxis(bizParkingSpace.getTransverseAxis());
		from.setPortraiAxis(bizParkingSpace.getPortraiAxis());
		from.setIsOccupy(bizParkingSpace.getIsOccupy());
		from.setSelfIsShare(bizParkingSpace.getSelfIsShare());
		from.setSpacePosition(bizParkingSpace.getSpacePosition());
		from.setDepartmentId(bizParkingSpace.getDepartmentId());
		from.setIsDeleted(bizParkingSpace.getIsDeleted());
		from.setPlatformIsShare(bizParkingSpace.getPlatformIsShare());
		from.setShareChangeTime(bizParkingSpace.getShareChangeTime());
		from.setReserveTempCount(bizParkingSpace.getReserveTempCount());
		from.setReserveTempMaxCount(bizParkingSpace.getReserveTempMaxCount());
		from.setRankDirection(bizParkingSpace.getRankDirection());
		from.setRankOrder(bizParkingSpace.getRankOrder());
		from.setCreator(bizParkingSpace.getCreator());
		from.setCreateTime(bizParkingSpace.getCreateTime());
		from.setModifier(bizParkingSpace.getModifier());
		from.setModifyTime(bizParkingSpace.getModifyTime());

		return from;
	}

	
	public BizParkingSpaceFrom findById(String id) {
		BizParkingSpace entity = this.getOne(new QueryWrapper<BizParkingSpace>().lambda()
				.eq(BizParkingSpace::getId, id)
				);
		if(entity == null) return null;
		return transformationBean(entity);
	}
	
	public ResponseData remove(String id) {
		if(findById(id)== null) {
			return ResponseData.fail("没有找到对应记录");
		}
		BizParkingSpace bizParkingSpace = new BizParkingSpace();
		bizParkingSpace.setId(id);
		this.updateById(bizParkingSpace);
		return ResponseData.success("删除成功");
	}
	
	public IPage pageList(Integer page, Integer size, BizParkingSpaceFrom ibean) {
		return this.page(new Page(page, size), new QueryWrapper<BizParkingSpace>().lambda()
				.eq(StringUtils.isNotEmpty(ibean.getPlCode()), BizParkingSpace::getPlCode, ibean.getPlCode())
				.eq(StringUtils.isNotEmpty(ibean.getSpaceCode()), BizParkingSpace::getSpaceCode, ibean.getSpaceCode())
				.eq(StringUtils.isNotEmpty(ibean.getZoneCode()), BizParkingSpace::getZoneCode, ibean.getZoneCode())
				.eq(StringUtils.isNotEmpty(ibean.getPlZoneInZoneCode()), BizParkingSpace::getPlZoneInZoneCode, ibean.getPlZoneInZoneCode())
				.eq(StringUtils.isNotEmpty(ibean.getPropertyPerson()), BizParkingSpace::getPropertyPerson, ibean.getPropertyPerson())
				.eq(StringUtils.isNotEmpty(ibean.getUserPerson()), BizParkingSpace::getUserPerson, ibean.getUserPerson())
				.eq(StringUtils.isNotEmpty(ibean.getUserPhone()), BizParkingSpace::getUserPhone, ibean.getUserPhone())
				.eq(StringUtils.isNotEmpty(ibean.getNature()), BizParkingSpace::getNature, ibean.getNature())
				.eq(StringUtils.isNotEmpty(ibean.getIsShare()), BizParkingSpace::getIsShare, ibean.getIsShare())
				.eq(StringUtils.isNotEmpty(ibean.getSpaceType()), BizParkingSpace::getSpaceType, ibean.getSpaceType())
				.eq(StringUtils.isNotEmpty(ibean.getSpaceState()), BizParkingSpace::getSpaceState, ibean.getSpaceState())
				.eq(StringUtils.isNotEmpty(ibean.getRemark()), BizParkingSpace::getRemark, ibean.getRemark())
				.eq(ibean.getTransverseAxis() != null, BizParkingSpace::getTransverseAxis, ibean.getTransverseAxis())
				.eq(ibean.getPortraiAxis() != null, BizParkingSpace::getPortraiAxis, ibean.getPortraiAxis())
				.eq(StringUtils.isNotEmpty(ibean.getIsOccupy()), BizParkingSpace::getIsOccupy, ibean.getIsOccupy())
				.eq(StringUtils.isNotEmpty(ibean.getSelfIsShare()), BizParkingSpace::getSelfIsShare, ibean.getSelfIsShare())
				.eq(StringUtils.isNotEmpty(ibean.getSpacePosition()), BizParkingSpace::getSpacePosition, ibean.getSpacePosition())
				.eq(ibean.getDepartmentId() != null, BizParkingSpace::getDepartmentId, ibean.getDepartmentId())
				.eq(StringUtils.isNotEmpty(ibean.getIsDeleted()), BizParkingSpace::getIsDeleted, ibean.getIsDeleted())
				.eq(StringUtils.isNotEmpty(ibean.getPlatformIsShare()), BizParkingSpace::getPlatformIsShare, ibean.getPlatformIsShare())
				.eq(ibean.getShareChangeTime() != null, BizParkingSpace::getShareChangeTime, ibean.getShareChangeTime())
				.eq(ibean.getReserveTempCount() != null, BizParkingSpace::getReserveTempCount, ibean.getReserveTempCount())
				.eq(ibean.getReserveTempMaxCount() != null, BizParkingSpace::getReserveTempMaxCount, ibean.getReserveTempMaxCount())
				.eq(StringUtils.isNotEmpty(ibean.getRankDirection()), BizParkingSpace::getRankDirection, ibean.getRankDirection())
				.eq(ibean.getRankOrder() != null, BizParkingSpace::getRankOrder, ibean.getRankOrder())
				.eq(ibean.getCreator() != null, BizParkingSpace::getCreator, ibean.getCreator())
				.eq(ibean.getCreateTime() != null, BizParkingSpace::getCreateTime, ibean.getCreateTime())
				.eq(ibean.getModifier() != null, BizParkingSpace::getModifier, ibean.getModifier())
				.eq(ibean.getModifyTime() != null, BizParkingSpace::getModifyTime, ibean.getModifyTime())
				);
	}

	public List<BizParkingSpace> oneList(String plCode, String spaceCode){
		QueryWrapper<BizParkingSpace> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("pl_code",plCode);
		queryWrapper.eq("space_code",spaceCode);
		return list(queryWrapper);
	}

	@DataSource(value = DataSourceType.SLAVE)
	public Page<ParkingSpaceDTO> parkingSpaceList(ParkingSpaceParam parkingSpaceParam){
		Page<ParkingSpaceDTO> page = new Page<>(parkingSpaceParam.getPage(),parkingSpaceParam.getSize());
		Page<ParkingSpaceDTO> page1 = bizParkingSpaceMapper.parkingSpaceList(page,parkingSpaceParam.getPlCode(),parkingSpaceParam.getSpaceCode(),parkingSpaceParam.getSpaceType());
		return page1;
	}


	@DataSource(value = DataSourceType.SLAVE)
	public BizParkingSpace deleteParkingSpace(String id){
		BizParkingSpace bizParkingSpace = getById(id);
		if(bizParkingSpace!=null){
			List<BizParkingSpace>	bizParkingSpaces = list(new QueryWrapper<BizParkingSpace>().lambda().eq(BizParkingSpace::getPlCode,bizParkingSpace.getPlCode()).eq(BizParkingSpace::getSpaceCode,bizParkingSpace.getSpaceCode()));
			if(bizParkingSpaces !=null){
				for (BizParkingSpace bizParkingSpace1:bizParkingSpaces){
					List<BizPriceParkingSpace> bizPriceParkingSpaces = bizPriceParkingSpaceService.oneList(bizParkingSpace1.getPlCode(),bizParkingSpace1.getSpaceCode());
					if(bizPriceParkingSpaces !=null){
						bizPriceParkingSpaces.stream().map(a->{

							return null;
						}).collect(Collectors.toList());
					}
				}
			}
		}
		return null;
	}

}
